﻿using ScottPlot.Drawing.Colormaps;
using ScottPlot;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Collections.ObjectModel;

namespace XH.BMS.Base
{
    public class ScottPlotExtension
    {

        #region 数据源附加属性

        public static ObservableCollection<ObservableCollection<double>> GetDataSource(DependencyObject obj)
        {
            return (ObservableCollection<ObservableCollection<double>>)obj.GetValue(DataSourceProperty);
        }

        public static void SetDataSource(DependencyObject obj, ObservableCollection<ObservableCollection<double>> value)
        {
            obj.SetValue(DataSourceProperty, value);
        }
        // Using a DependencyProperty as the backing store for DataSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.RegisterAttached(
                "DataSource",
                typeof(ObservableCollection<ObservableCollection<double>>),
                typeof(ScottPlotExtension),
                new PropertyMetadata(null, new PropertyChangedCallback(OnDataSourceChanged)));

        #endregion

        private static void OnDataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // 对象 
            WpfPlot plot = (WpfPlot)d;
            if (e.NewValue != null)
            {
                // 数据
                ObservableCollection<ObservableCollection<double>> values = (ObservableCollection<ObservableCollection<double>>)e.NewValue;
                values.CollectionChanged += (se, ev) =>
                {
                    plot.plt.Clear();
                    foreach (var item in values)
                    {
                        item.CollectionChanged += (ss, ee) =>
                        {
                            plot.plt.Clear();
                            foreach (var item in values)
                            {
                                plot.plt.AddSignal(item.ToArray());
                            }
                            plot.Refresh();
                        };
                        plot.plt.AddSignal(item.ToArray());
                    }
                    plot.Refresh();
                };

                plot.plt.Clear();
                foreach (var item in values)
                {
                    // 监听这个序列的点值变化
                    item.CollectionChanged += (ss, ee) =>
                    {
                        plot.plt.Clear();
                        foreach (var item in values)
                        {
                            plot.plt.AddSignal(item.ToArray());
                        }
                        plot.Refresh();
                    };
                    plot.plt.AddSignal(item.ToArray());
                }
                //plot.plt.RemoveAt(0);
                plot.Refresh();
            }
        }
    }
}
